// pages/logout-last/logout-last.js
// const app = getApp()
// import user from '../../utils/user.js'
import {
    getInfoByIdCardApi,
    logoutAccountApi
} from '../../api/logout-last'
import {
    getSendVerifyCode,
    getCheckVerifyCode
} from '../../api/app.js'
import {
    dateFormat,
} from '../../utils/util'
Page({

    /**
     * 页面的初始数据
     */
    data: {
        countDownNum: 60, // 倒计时初始值为 60 秒
        countingDown: false, // 是否正在倒计时的标志
        infoMsg: {},
        phone: '',
        tabList: [1, 2, 3],
        current: 1,
        code: '',
        verifyCode: '',
        codes: [],
        countDownTimer: null, // 定时器标识符
    },

    /**
     * 生命周期函数--监听页面加载
     */
    async onLoad(options) {
        console.log(options, 'optionsoptions')
        // this.getInfoByIdCard(options.idCardNo)
        options.idCardNo && await this.getInfoByIdCard(options.idCardNo)
        options?.step && (this.setData({ current: Number(options?.step)}))
        options?.originPhone && (this.setData({ ['infoMsg.phone']: options.originPhone }))
        if (this.data.current === 2) {
            this.getSendVerifyCode()
        }
    },
    async getInfoByIdCard(idCardNo) {
        let res = await getInfoByIdCardApi({
            idCardNo: idCardNo
        })
        console.log(res, 'ibfysgyhi')
        this.setData({
            infoMsg: res
        })
    },
    // 获取验证码
    async getSendVerifyCode() {
        const nowYear = dateFormat('YYYY', new Date())
        const nowMonth = dateFormat('mm', new Date())
        await getSendVerifyCode({
            phone: Number(this.data.infoMsg.phone) + 1001010086 + ((Number(nowYear) + Number(nowMonth)) * 1000000)
        })
        await this.startCountDown()
    },
    startCountDown: function () { // 开始倒计时
        if (this.data.countingDown) return // 如果正在倒计时，则直接返回
        this.setData({
            countingDown: true
        }) // 设置正在倒计时的标志

        let timer = setInterval(() => { // 每秒钟更新倒计时
            let newCountDownNum = this.data.countDownNum - 1
            if (newCountDownNum >= 0) { // 如果还未到 0 秒，则更新倒计时
                this.setData({
                    countDownNum: newCountDownNum
                })
            } else { // 如果已经到 0 秒，则取消定时器并重置倒计时
                clearInterval(this.data.countDownTimer)
                this.setData({
                    countDownNum: 60,
                    countingDown: false
                })
            }
        }, 1000)

        this.setData({
            countDownTimer: timer
        }) // 设置定时器标识符
    },
    stopCountDown: function () { // 停止倒计时
        clearInterval(this.data.countDownTimer) // 取消定时器
        this.setData({
            countDownNum: 60,
            countingDown: false
        }) // 重置倒计时和标志
    },
    inputNumber(event) {
        // 将密码分为多个字符
        this.setData({
            ['codes']: event.detail.value.split(''),
        })
        this.setData({
            verifyCode: event.detail.value.slice(0, 6)
        })
        // 字符达到6个自动下一步
        // if (event.detail.value.length === 6) {
        //     this.setData({
        //         verifyCode: event.detail.value.slice(0, 6)
        //     })
        //     this.getCheckVerifyCode()
        // }
    },
    async onSureLogout() {
        let res = await this.getCheckVerifyCode()
        if (res) {
            this.logoutAccount()
        }
    },
    async logoutAccount() {
        await logoutAccountApi({idCardNo: this.data.infoMsg.idCardNo})
        wx.reLaunch({
        })
        await this.onToThird()
    },
    // 验证码校验
    async getCheckVerifyCode() {
        let res = await getCheckVerifyCode({
            phone: Number(this.data.infoMsg.phone),
            verifyCode: this.data.verifyCode
        })
        return res
    },
    onToSecond() {
        this.setData({
            current: 2
        })
        this.getSendVerifyCode()
    },
    onToThird() {
        this.setData({
            current: 3
        })
    },
    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {

    },
    onToIndex() {
        wx.redirectTo({
            url: '/pages/login/login',
        })
    },
    onToOtherWay() {
        console.log('其他申诉方式')
        wx.navigateTo({
            url: '/pages/appeal/appeal?idCardNo=' + this.data.infoMsg.idCardNo,
        })
    },
    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {
        this.stopCountDown()
    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage() {

    }
})